package com.zzsy.carosshopprojects.activitys.home.mainpage.orderamount;

import android.content.Context;
import android.os.Bundle;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.king.base.util.ToastUtils;
import com.zzsy.carosshopprojects.R;
import com.zzsy.carosshopprojects.activitys.home.mainpage.barmanager.common.RechargeSuccessMode;
import com.zzsy.carosshopprojects.base.BaseFragment;
import com.zzsy.carosshopprojects.bean.RechargeOrderDetialBean;
import com.zzsy.carosshopprojects.util.ConverterHelper;
import com.zzsy.carosshopprojects.util.FormatTimeUtil;
import com.zzsy.carosshopprojects.util.StringUtils;
import com.zzsy.carosshopprojects.util.sunmiprint.PrintHelper;

import java.math.BigDecimal;

import butterknife.BindView;
import butterknife.OnClick;

/**
 * 充值订单详情
 */
public class OrderReghterAmountDetailFragment extends BaseFragment {
    public static OrderReghterAmountDetailFragment newInstance(String orderNum) {
        OrderReghterAmountDetailFragment orderReghterAmountDetailFragment = new OrderReghterAmountDetailFragment();
        Bundle bundle = new Bundle();
        bundle.putString("orderNum", orderNum);
        orderReghterAmountDetailFragment.setArguments(bundle);
        return orderReghterAmountDetailFragment;
    }

    @BindView(R.id.tv_station_name)
    TextView tvStationName;
    @BindView(R.id.tv_reghter_amount)
    TextView tvReghterAmount;
    @BindView(R.id.tv_order_nums)
    TextView tvOrderNums;
    @BindView(R.id.tv_pay_type)
    TextView tvPayType;
    @BindView(R.id.tv_pay_time)
    TextView tvPayTime;
    @BindView(R.id.tv_fp_state)
    TextView tvFPState;
    @BindView(R.id.tv_member_phone)
    TextView tvMmeberPhone;
    @BindView(R.id.tv_member_name)
    TextView tvMemberRealName;
    @BindView(R.id.ll_phone)
    LinearLayout llPhone;
    @BindView(R.id.ll_name)
    LinearLayout llName;

    private String orderNumber;
    private String oilType = "";
    private String payTypeStr;
    private String memberPhone;
    private double orderAmountDou;
    private double memberBlanceAmountsDou;
    private String time;
    private String storeName;
    private double redPackedAmountDou;
    private double goodsCouponAmountDou;
    private double redPackedSumDou;
    private String reductionCouponAmount;
    private RechargeSuccessMode rechargeSuccessMode;
    private RechargeOrderDetialBean beans;

    @Override
    protected int initLayout() {
        return R.layout.fg_orderreghteramountdetail;
    }

    @Override
    protected void initView(View view) {
        initTitle(view, getResources().getString(R.string.OrderDetail), getActivity());
        rechargeSuccessMode = getViewModelProvider().get(RechargeSuccessMode.class);
    }

    @Override
    protected void initData(Context mContext) {
        String orderNumber = getArguments().getString("orderNum");
        if (!StringUtils.isEmpty(orderNumber)) {
            rechargeSuccessMode.getOrderDetialData(getContext(), orderNumber);
        } else {
            ToastUtils.showToast(getContext(), "订单号异常！");
            getActivity().finish();
        }
    }

    @Override
    protected void loadMethod() {
        getOrderDetialData();
    }

    @OnClick({R.id.iv_phone_bar})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.iv_phone_bar:
                if (!StringUtils.isEmpty(orderNumber)) {
                    print();
                } else {
                    ToastUtils.showToast(context.getApplicationContext(), "订单数据异常,无法打印小票！");
                    getActivity().finish();
                }
                break;

        }
    }

    /**
     * 处理订单详情查询结果
     *
     * @param
     */
    private void getOrderDetialData() {
        rechargeSuccessMode.getLiveData().observe(this, bean -> {
            beans = bean;
            setData();
        });
    }

    /**
     * 赋值
     */
    public void setData() {
        if (beans != null) {
            orderNumber = beans.getRechargeNumber();
            String orderCreateTime = beans.getOrderCreateTime();
            if (!StringUtils.isEmpty(orderCreateTime)) {
                time = FormatTimeUtil.formatTimeStamp2DateStr(String.valueOf(Long.parseLong(orderCreateTime) / 1000), "yyyy-MM-dd HH:mm:ss");
            }
            String orderAmount = beans.getAmount();

            storeName = beans.getStoreName();
            if (StringUtils.isEmpty(storeName)) {
                storeName = "";
            }
            if (beans.getProductId() != null) {
                if (beans.getProductId().equals("1")) {
                    oilType = "柴油";
                } else if (beans.getProductId().equals("2")) {
                    oilType = "汽油";
                } else if (beans.getProductId().equals("25")) {
                    oilType = "LNG";
                } else if (beans.getProductId().equals("26")) {
                    oilType = "CNG";
                }
            }


            memberPhone = beans.getPhone();
            if (StringUtils.isEmpty(memberPhone)) {
                memberPhone = "";
            }
            String payType = beans.getPayType();
            if (!StringUtils.isEmpty(payType)) {
                if ("1".equals(payType)) {
                    payTypeStr = "支付宝支付";
                } else if ("2".equals(payType)) {
                    payTypeStr = "微信支付";
                } else if ("3".equals(payType)) {
                    payTypeStr = "银联支付";
                } else if ("4".equals(payType)) {
                    payTypeStr = "会员支付";
                } else if ("5".equals(payType)) {
                    payTypeStr = "现金支付";
                }
            }

            int invoiceStatus = beans.getInvoiceStatus();
            String invoiceStr = null;
            if (invoiceStatus == 1) {
                invoiceStr = "已开票";
            } else if (invoiceStatus == 0) {
                invoiceStr = "未开票";
            } else if (invoiceStatus == 2) {
                invoiceStr = "不可开";
            }
            String realName = beans.getRealName();
            if (StringUtils.isEmpty(realName)) {
                realName = "";
            }

                      /*  String memberBlanceAmounts = baseBean.getData().getMemberBlanceAmounts();
                        String memberCouponAmounts = baseBean.getData().getMemberCouponAmounts();
                        String couponBalance = baseBean.getData().getCouponBalance();*/
            //暂时显示
            String memberBlanceAmounts = beans.getRechargeBalance();
            if (StringUtils.isEmpty(memberBlanceAmounts)) {
                memberBlanceAmounts = "0";
            }

            String goodsCouponAmount = beans.getGoodsCouponAmount();
            if (StringUtils.isEmpty(goodsCouponAmount)) {
                goodsCouponAmount = "0";
            }

            String redPackedAmount = beans.getRedPackedAmount();
            String redPackedSum = beans.getRedPackedSum();
            if (StringUtils.isEmpty(redPackedAmount)) {
                redPackedAmount = "0";
            }
            redPackedAmountDou = ConverterHelper.toBigDecimal(redPackedAmount).divide(ConverterHelper.toBigDecimal(100)).setScale(2, BigDecimal.ROUND_HALF_UP).doubleValue();
            redPackedSumDou = ConverterHelper.toBigDecimal(redPackedSum).divide(ConverterHelper.toBigDecimal(100)).setScale(2, BigDecimal.ROUND_HALF_UP).doubleValue();
            memberBlanceAmountsDou = ConverterHelper.toBigDecimal(memberBlanceAmounts).divide(ConverterHelper.toBigDecimal(100)).setScale(2, BigDecimal.ROUND_HALF_UP).doubleValue();
            orderAmountDou = ConverterHelper.toBigDecimal(orderAmount).divide(ConverterHelper.toBigDecimal(100)).setScale(2, BigDecimal.ROUND_HALF_UP).doubleValue();
            goodsCouponAmountDou = ConverterHelper.toBigDecimal(goodsCouponAmount).divide(ConverterHelper.toBigDecimal(100)).setScale(2, BigDecimal.ROUND_HALF_UP).doubleValue();
            setUI(storeName, orderAmountDou, orderNumber, payTypeStr, time, invoiceStr, memberPhone, realName);

        }
    }

    /**
     * 设置UI
     */
    private void setUI(String storeName, double orderAmountDou, String orderNumber, String payTypeStr, String time, String invoiceStr, String memberPhone, String realName) {
        tvOrderNums.setText(orderNumber);
        tvStationName.setText(storeName);
        tvReghterAmount.setText("￥" + orderAmountDou);
        tvPayType.setText("" + payTypeStr);
        tvPayTime.setText("" + time);
        tvFPState.setText("" + invoiceStr);

        if (StringUtils.isEmpty(memberPhone)) {
            llPhone.setVisibility(View.GONE);
        } else {
            llPhone.setVisibility(View.VISIBLE);
            tvMmeberPhone.setText(memberPhone);

        }
        if (StringUtils.isEmpty(realName)) {
            llName.setVisibility(View.GONE);
        } else {
            llName.setVisibility(View.VISIBLE);
            tvMemberRealName.setText(realName);
        }

    }

    /**
     * 重打印小票
     */
    private void print() {
        PrintHelper.getInstance().PrintRechargeOrder(orderNumber, time, orderAmountDou + "", storeName, payTypeStr, memberBlanceAmountsDou, memberPhone,
                redPackedAmountDou, goodsCouponAmountDou, redPackedSumDou, oilType, beans.getReductionCouponAmount(), true);
    }
}
